import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

import '../../components/custom_toast.dart';
import '../../config/service_url.dart';
import '../../models/base_model.dart';
import '../../utils/http_util.dart';
import '../../utils/my_colors.dart';

class RegisterPage extends StatefulWidget {
  const RegisterPage({super.key});

  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  final TextEditingController _usernameController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();
  final TextEditingController _confirmController = TextEditingController();
  bool _isRememberMeChecked = true; // 假设默认记住我是选中的

  //提交登录
  void _submitRegisterData() async {
    // 检查用户名是否为空
    if (_usernameController.text.isEmpty) {
      CustomToast.showWarningToast("用户名不能为空！");
      return; // 用户名为空，直接返回
    }

    // 检查密码是否为空
    if (_passwordController.text.isEmpty) {
      CustomToast.showWarningToast("密码不能为空！");
      return; // 密码为空，直接返回
    }

    // 检查确认密码是否为空
    if (_confirmController.text.isEmpty) {
      CustomToast.showWarningToast("请确认密码！");
      return; // 密码为空，直接返回
    }

    if (_passwordController.text != _confirmController.text) {
      CustomToast.showWarningToast("两次输入的密码不一致！");
      return;
    }

    Map<String, dynamic> registerMap = <String, dynamic>{};
    registerMap["username"] = _usernameController.value.text;
    registerMap["password"] = _passwordController.value.text;
    Response result = await HttpUtil.post(register, data: registerMap);

    BaseModel baseResponse = BaseModel.fromJson(result.data);
    if(baseResponse.code == 1){
      CustomToast.showSuccessToast("注册成功！");
    } else {
      CustomToast.showErrorToast(baseResponse.msg!);
    }

    // //保存登录凭证token
    // SharedPreferencesUtil.saveString(token, "${loginModel.data.tokenHead} ${loginModel.data.token}");
  }

  void _toggleRememberMe(bool? newValue) {
    setState(() {
      _isRememberMeChecked = newValue!;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
      ),
      body: Stack(
        children: [
          Container(
            decoration: const BoxDecoration(
              image: DecorationImage(
                image: AssetImage('assets/images/bg2.png'),
                fit: BoxFit.cover,
              ),
            ),
            child: Center(
              child: SingleChildScrollView( // 添加滚动视图以适应小屏幕
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    SizedBox(height: 20),
                    Text(
                      '注册',
                      style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: MyColors.white),
                    ),
                    SizedBox(height: 20),
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 40),
                      child: TextField(
                        style: TextStyle(color: MyColors.white, fontSize: 20),
                        controller: _usernameController,
                        cursorColor: MyColors.white,
                        decoration: InputDecoration(
                          hintText: '请输入您的账号',
                          hintStyle: TextStyle(color: MyColors.white),
                          prefixIcon: Icon(Icons.email, color: MyColors.white),
                          border: OutlineInputBorder(
                            borderSide: BorderSide.none,
                            borderRadius: BorderRadius.circular(10),
                          ),
                          filled: true,
                          fillColor: MyColors.input.withOpacity(0.7), // 背景色修改为灰色透明
                          contentPadding: EdgeInsets.all(10),
                        ),
                      ),
                    ),
                    SizedBox(height: 10),
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 40),
                      child: TextField(
                        style: TextStyle(color: MyColors.white, fontSize: 20),
                        controller: _passwordController,
                        cursorColor: MyColors.white,
                        decoration: InputDecoration(
                          hintText: '请输入您的密码',
                          hintStyle: TextStyle(color: MyColors.white),
                          prefixIcon: Icon(Icons.lock, color: MyColors.white),
                          border: OutlineInputBorder(
                            borderSide: BorderSide.none,
                            borderRadius: BorderRadius.circular(10),
                          ),
                          filled: true,
                          fillColor: MyColors.input.withOpacity(0.7), // 背景色修改为灰色透明
                          contentPadding: EdgeInsets.all(10),
                        ),
                        obscureText: true,
                      ),
                    ),
                    SizedBox(height: 10),
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 40),
                      child: TextField(
                        style: TextStyle(color: MyColors.white, fontSize: 20),
                        controller: _confirmController,
                        cursorColor: MyColors.white,
                        decoration: InputDecoration(
                          hintText: '请再次确认密码',
                          hintStyle: TextStyle(color: MyColors.white),
                          prefixIcon: Icon(Icons.lock, color: MyColors.white),
                          border: OutlineInputBorder(
                            borderSide: BorderSide.none,
                            borderRadius: BorderRadius.circular(10),
                          ),
                          filled: true,
                          fillColor: MyColors.input.withOpacity(0.7), // 背景色修改为灰色透明
                          contentPadding: EdgeInsets.all(10),
                        ),
                        obscureText: true,
                      ),
                    ),
                    SizedBox(height: 10),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        ElevatedButton(
                          onPressed: _submitRegisterData, // 更新这里
                          style: ButtonStyle(
                            backgroundColor: MaterialStateProperty.all<Color>(MyColors.girlBlue),
                            padding: MaterialStateProperty.all(EdgeInsets.symmetric(vertical: 12, horizontal: 24)),
                          ),
                          child: Text('注册', style: TextStyle(color: MyColors.white, fontSize: 16)),
                        ),
                        SizedBox(width: 10),
                        ElevatedButton(
                          onPressed: () {
                            Navigator.pushNamed(context, '/login');
                          },
                          style: ButtonStyle(
                            backgroundColor: MaterialStateProperty.all<Color>(MyColors.green),
                            padding: MaterialStateProperty.all(EdgeInsets.symmetric(vertical: 12, horizontal: 24)),
                          ),
                          child: Text('登录', style: TextStyle(color: MyColors.white, fontSize: 16)),
                        ),
                      ],
                    ),
                    SizedBox(height: 10),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Checkbox(
                          value: _isRememberMeChecked,
                          onChanged: _toggleRememberMe,
                        ),
                        Text(
                          '记住我',
                          style: TextStyle(color: MyColors.white),
                        ),
                        TextButton(
                          onPressed: () {
                            //忘记密码
                          },
                          child: Text('忘记密码?', style: TextStyle(color: MyColors.white)),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}